import { _decorator, Component, Node, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SheepMove')
export class SheepMove extends Component {

    @property([Node])
    movePointTrans: Node[] = [];
    start() {
        if (this.movePointTrans.length > 0) {
            this.moveAlongPath();
        }
    }

    moveAlongPath() {
        let points = this.movePointTrans.map(node => node.getPosition());
        let curMovePointIndex = 0;

        const moveToNextPoint = () => {
            curMovePointIndex = (curMovePointIndex + 1) % points.length;
            let targetPoint = points[curMovePointIndex];
            tween(this.node)
                .to(1, { position: targetPoint }, { easing: "sineInOut" })
                .call(moveToNextPoint)
                .start()
        }
        moveToNextPoint();
    }

    update(deltaTime: number) {
        if (this.node.position.y > 0) {
            this.node.setScale(-1, 1)
        } else {
            this.node.setScale(1, 1)
        }
    }
}


